package com.kronos.gamestates;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.Texture;
import com.kronos.audio.MusicStream;
import com.kronos.game.KronosGame;
import com.kronos.managers.GameStateManager;

public class IntroState extends GameState {
	private Texture img;
	
	private int alpha;
	private int ticks;
	
	private final int FADE_IN = 85;
	private final int LENGTH = 85;
	private final int FADE_OUT = 85; 
	
	public IntroState(GameStateManager gsm) {
		super(gsm);
		img = KronosGame.manager.getTexture("introscreen.jpg");
		ticks = 0;
		MusicStream.playSound("intro");
		MusicStream.loopSound("intro", true);
	}

	@Override
	public void update() {
		ticks++;
		if(ticks < FADE_IN) {
			alpha = (int) (255 - 255 * (1.0 * ticks / FADE_IN));
			if(alpha < 0) alpha = 0;
		
		}
		if(ticks > FADE_IN + LENGTH) {
			alpha = (int) (255 * (1.0 * ticks - FADE_IN - LENGTH) / FADE_OUT);
			if(alpha > 255) alpha = 255;
		}
		
		if(ticks > FADE_IN + LENGTH + FADE_OUT) {
			KronosGame.manager.removeTexture("introscreen.jpg");
			img.dispose();
			gsm.setState(GameStateManager.LOGIN_STATE);
		}  
	}

	@Override
	public void render() {
		sb.begin();
		sb.setColor(1.0f, 1.0f, 1.0f, alpha);
		Gdx.gl.glClearColor(0, 0, 0, 1);
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
		sb.draw(img, KronosGame.V_WIDTH/2  - img.getWidth()/2,  KronosGame.V_HEIGHT/2 - img.getHeight()/2);
		sb.end();
	}

	@Override
	public void dispose() {
		sb.dispose();
		img.dispose();
		if(MusicStream.isPlaying("intro")) {
			MusicStream.disposeSound("intro");
		}
	}
}
